﻿Imports System.Windows.Forms.ListBox

Public Class CustomObjectCollection
	Inherits ObjectCollection

	Public Event ItemAdded As EventHandler(Of ListBoxItemEventArgs)
	Public Event ItemAdding As EventHandler(Of ListBoxItemEventArgs)

	Public Sub New(owner As System.Windows.Forms.ListBox)
		MyBase.New(owner)
	End Sub

	Public Sub New(owner As System.Windows.Forms.ListBox, value() As Object)
		MyBase.New(owner, value)
	End Sub

	Public Sub New(owner As System.Windows.Forms.ListBox, value As System.Windows.Forms.ListBox.ObjectCollection)
		MyBase.New(owner, value)
	End Sub

	Public Overloads Function Add(item As Object) As Integer
		Dim e As New ListBoxItemEventArgs
		e.Item = item

		RaiseEvent ItemAdding(Me, e)

		Dim returnValue = -1
		If Not e.Cancel Then
			returnValue = MyBase.Add(item)
			RaiseEvent ItemAdded(Me, e)
		End If

		Return returnValue
	End Function

	Public Overloads Sub AddRange(items() As Object)
		Dim e As New ListBoxItemEventArgs
		e.Item = items

		RaiseEvent ItemAdding(Me, e)

		If Not e.Cancel Then
			MyBase.AddRange(items)
			RaiseEvent ItemAdded(Me, e)
		End If
	End Sub

	Public Overloads Sub AddRange(value As System.Windows.Forms.ListBox.ObjectCollection)
		Dim e As New ListBoxItemEventArgs
		e.Item = value

		RaiseEvent ItemAdding(Me, e)

		If Not e.Cancel Then
			MyBase.AddRange(value)
			RaiseEvent ItemAdded(Me, e)
		End If
	End Sub
End Class

Public Class ListBoxItemEventArgs
	Inherits System.ComponentModel.CancelEventArgs

	Public Property Item As Object
End Class